/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.charge.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IBusinessCodeDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：交易码管理<br>
 * @author 罗军林 创建日期：2008-7-31
 */
public class BusinessCodeDaoImpl extends PmsBaseDaoImpl<BusinessCodeDomain> implements IBusinessCodeDao {

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.dao.IBusinessCodeDao#queryBusinessCode(com.ce.pms.charge.vo.BusinessCodeVO, int, int)
	 */
	public Page queryBusinessCode(BusinessCodeVO condition, int pageNo, int pageSize) {
		StringBuffer sql=new StringBuffer("FROM BusinessCodeDomain business ");
		sql.append( buildOrderStr(condition) );
		return this.pagedQueryByPageNo(sql.toString(), pageNo, pageSize);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.dao.IBusinessCodeDao#countBusinessCodeBizCode( String, String)
	 */
	public Long countBusinessCodeBizCode(BusinessCodeVO condition){
		StringBuffer sql=new StringBuffer("select count(*) from BusinessCodeDomain business ");
		sql.append( buildCondition(condition) );
		Query query = this.createQuery(sql.toString());
		Object result = query.uniqueResult();
		return (result != null ? (Long)result : null);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.IBusinessCodeDao#getAllNotUseBusinessCodeList()
	 */
	public List<BusinessCodeDomain> getAllNotUseBusinessCodeList(String code) {
		StringBuffer sql=new StringBuffer("FROM BusinessCodeDomain business ")
		.append(" WHERE 1=1 ")
		.append(" and business.keyword NOT IN(SELECT param.pvalue FROM ParameterDefDomain param ")
		.append(" where param.gkey='").append(ParameterDefDomain.GKEY_CHARGE).append("'")
		.append(" and param.pvalue IS NOT NULL )")
		.append(" and business.keyword NOT IN(")
		.append(" select charge.bizCode.keyword FROM ChargeItemDomain charge) ")
		.append(builderConditionCode(code));
		Query query = createQueryByCode(code, sql);

		return query.list();
	}

	/**
	 * 排除条件: 1 交易码表中类型为收入类型且以被收费项目绑定的交易码
	 *         2 交易码表中有一个交易码ID被参数设置表中的滞纳金项目引用
	 */
	public List<BusinessCodeVO> getDebitNotUseBusinessCodeList(String exceptId) {
		StringBuffer sql = new StringBuffer("SELECT * FROM FEE_BUSINESS_CODE WHERE BIZ_CODE_ID NOT IN (SELECT  * FROM ( SELECT * FROM (SELECT BIZ_CODE_ID  FROM FEE_CHARGE_ITEM UNION SELECT PVALUE  FROM SEC_PARAMETER_DEF WHERE GKEY='4' AND PKEY='LATEFEE_CODE') B ");
		if(!BlankUtil.isBlank(exceptId))
			sql.append("WHERE B.BIZ_CODE_ID != ").append(exceptId);
		sql.append(" )  A  WHERE A.BIZ_CODE_ID IS NOT NULL) AND DEBIT_OR_CREDIT='001'");
		Iterator results = createSQLQuery(sql.toString(), null).list().iterator();
		List<BusinessCodeVO> list = new ArrayList<BusinessCodeVO>();
		while (results.hasNext())
		{
		    Object[] rows = (Object[]) results.next();
		    BusinessCodeVO businessCode = new BusinessCodeVO();
		    businessCode.setKeyword(((BigInteger)rows[0]).longValue());
		    businessCode.setBizCode((String)rows[1]);
		    businessCode.setDescription((String)rows[2]);
		    list.add(businessCode);
		}
		return list;
	}

//	/**
//	 * 功能描述:查找支付类型指定支付方式的交易码<br>
//	 * @param payMethod
//	 * @return<br>
//	 * Create author:罗军林<br>
//	 * Create on:2008-9-28<br>
//	 * Edit author:<br>
//	 * Edit on:<br>
//	 * Why:<br>
//	 */
//	public BusinessCodeDomain findBusinessCodeByPayMethod(String payMethod){
//		String hql="FROM BusinessCodeDomain bc WHERE bc.debitOrCredit=? and bc.payMethod=?";
//		Query query=createQuery(hql, SysrefcodeDO.FEE_BUSINESS_CODE_CREDIT,payMethod);
//		Object result=query.uniqueResult();
//		return result==null ? null : (BusinessCodeDomain)result;
//	}

	/**
	 * 功能描述:查找指定参数预设的CODE的交易码<br>
	 * @param code
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-13<br>
	 * Edit author:<br>
	 * Edit on:增加了gkey='4'的条件<br>
	 * Why:<br>
	 */
	public BusinessCodeDomain findBusinessCodeByParaDef(String code){
		String hql="FROM BusinessCodeDomain bc WHERE bc.keyword in (SELECT p.pvalue FROM ParameterDefDomain p WHERE p.pkey=? AND p.gkey='4')";
		Query query=createQuery(hql, code);
		return (BusinessCodeDomain)query.uniqueResult();
	}
	/**
	 * 功能描述: 获取所有收入类型的交易码<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BusinessCodeDomain> getAllIncomeBusinessCode(){
		String queryStr = "FROM BusinessCodeDomain bc WHERE bc.debitOrCredit='001' ORDER BY bc.sysInfo.sysDate";
		Query query = super.createQuery(queryStr, new Object[]{});
		return query.list();
	}
	/**
	 * 功能描述:查询ID列表指定交易码<br>
	 * @param codeIds
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public List<BusinessCodeDomain> findBusinessCodeByIds(List<Long> codeIds){
		StringBuffer queryStr = new StringBuffer(" FROM BusinessCodeDomain bc ");
						queryStr.append(" WHERE 1=1 ");
						queryStr.append(" AND bc.keyword IN(:IDS)");
		Query query = super.createQuery(queryStr.toString(), null);
		query.setParameterList("IDS", codeIds);

		return query.list();
	}
	/** 功能描述:建立排序串<br>
	 * @param condition
	 * @param sql<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildOrderStr(BusinessCodeVO condition) {
		StringBuffer sql=new StringBuffer();
		if(condition != null && condition.getOrderStr() != null && condition.getOrderStr().trim() != ""){
			sql.append(" order by ").append(condition.getOrderStr());
		}else{
			sql.append(" order by ").append(" BIZ_CODE");
		}
		return sql.toString();
	}

	/**
	 * 功能描述:建立条件串<br>
	 * @param oldBizCode
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildCondition(BusinessCodeVO condition){
		StringBuffer result=new StringBuffer(" where 1=1 ");

		if(condition.getBizCode() != null && condition.getBizCode().trim() != ""){
			result.append(" and business.bizCode = '").append(condition.getBizCode()).append("'");
		}
		if(condition.getOldBizCode() != null && condition.getOldBizCode().trim() != ""){
			result.append(" and business.bizCode not in ('").append( condition.getOldBizCode() ).append("')");
		}
		return result.toString();
	}

	/**
	 * 功能描述:给code加上%,做模糊查询串<br>
	 * @param code
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String builderParamsCode(String code){
		return code == null || code.trim() == "" ? null : "%" + code +"%";
	}

	/**
	 * 功能描述:增加交易码查询条件<br>
	 * @param code
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String builderConditionCode(String code){
		return code == null || code.trim() == "" ? "" : " AND business.bizCode like ? ";
	}

	/** 功能描述:根据CODE建立QUERY<br>
	 * @param code
	 * @param sql
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Query createQueryByCode(String code, StringBuffer sql) {
		Query query = null;
		if(code == null || code.trim() == "" ){
			query = this.createQuery(sql.toString(), null);
		}else{
			query = this.createQuery(sql.toString(), builderParamsCode(code));
		}
		return query;
	}

	public List<BusinessCodeVO> getAllCreditBusinessCodeList() {
		String hql = "SELECT * FROM FEE_BUSINESS_CODE WHERE BIZ_CODE_ID NOT IN (SELECT PVALUE  FROM SEC_PARAMETER_DEF WHERE GKEY='4' AND PVALUE  IS NOT NULL AND PKEY IN('MULTIPLE_PAY_CODE','PRE_PAY_CODE','BANK_PAID_CODE')) AND DEBIT_OR_CREDIT='002'";
		Iterator results = createSQLQuery(hql, null).list().iterator();
		List<BusinessCodeVO> list = new ArrayList<BusinessCodeVO>();
		while (results.hasNext())
		{
		    Object[] rows = (Object[]) results.next();
		    BusinessCodeVO businessCode = new BusinessCodeVO();
		    businessCode.setKeyword(((BigInteger)rows[0]).longValue());
		    businessCode.setBizCode((String)rows[1]);
		    businessCode.setDescription((String)rows[2]);
		    list.add(businessCode);
		}
		return list;
	}


}
